<?php

namespace StudyBuddy\Controllers;

use \StudyBuddy\WebPage;
use \StudyBuddy\Request;
use \StudyBuddy\Responder;

/**
 * Controller for creating the page
 * with "Email options" form
 * as well as processing that form
 * 
 */
class Emailoptions extends WebPage
{
	protected $membersOnly = true;

	protected $permission = 'edit_profile';

	protected $layoutID = 1;

	/**
	 * @var object of type Form
	 */
	protected $oForm;


	/**
	 * @todo Translate strings used in form using the
	 * $this->oForm->name = val setting
	 *
	 * @todo maybe send email on save() notifying
	 * that email settings has been updated
	 *
	 * (non-PHPdoc)
	 * @see StudyBuddy.WebPage::main()
	 */
	protected function main(){

		$email = $this->oRegistry->Viewer->email;
		$this->oForm = new \StudyBuddy\Forms\EmailOptions($this->oRegistry);
		$this->oForm->formTitle = $this->aPageVars['title'] = $this->_('Your Email Subscription Preferences');
		$this->oForm->your_email = $email;

		if($this->oForm->isSubmitted()){
			$this->oRegistry->Dispatcher->post($this->oForm, 'onBeforeEmailOptionsUpdate');
			$this->savePrefs();
			$this->oRegistry->Dispatcher->post($this->oForm, 'onEmailOptionsUpdate');
			$this->aPageVars['body'] = '<div id="tools"><h3>Your email subscription preferences have been updated.</h3><p><a href="/emailoptions/">Your email preferences</a></p></div>';

		} else {
			$this->setForm();
			$this->aPageVars['body'] = $this->oForm->getForm();
		}
	}


	/**
	 * Save Email preferences in
	 * Viewer object and call save() to store
	 * to Database right away.
	 *
	 * This will set values of e_fu, e_fq, e_ft in USERS to
	 * either true or false, so the value will not be null
	 * it may become false but it will exist - it will
	 * not be considered null anymore
	 *
	 * @return object $this
	 */
	protected function savePrefs(){

		$formVals = $this->oForm->getSubmittedValues();
		d('formVals: '.print_r($formVals, 1));
		$oViewer = $this->oRegistry->Viewer;

		$oViewer['ne_fu'] = (empty($formVals['e_fu']));
		$oViewer['ne_fq'] = (empty($formVals['e_fq']));
		$oViewer['ne_ft'] = (empty($formVals['e_ft']));
		$oViewer['ne_fa'] = (empty($formVals['e_fa']));
		$oViewer['ne_fc'] = (empty($formVals['e_fc']));
		$oViewer['ne_ok'] = (empty($formVals['e_ok']));

		$oViewer->save();

		return $this;
	}


	/**
	 * Set the "checked" values of check boxes
	 * to the ones in Viewer object
	 *
	 * Value is considered checked in it is
	 * not spefically set to false by user
	 * by default there is no value in USERS collection
	 * for these settings, so it will be returned
	 * as null (but not false) from Viewer object
	 *
	 * Enter description here ...
	 */
	protected function setForm(){

		$this->oForm->e_fu = (true !== $this->oRegistry->Viewer->ne_fu) ? 'checked' : '';
		$this->oForm->e_ft = (true !== $this->oRegistry->Viewer->ne_ft) ? 'checked' : '';
		$this->oForm->e_fq = (true !== $this->oRegistry->Viewer->ne_fq) ? 'checked' : '';
		$this->oForm->e_fa = (true !== $this->oRegistry->Viewer->ne_fa) ? 'checked' : '';
		$this->oForm->e_fc = (true !== $this->oRegistry->Viewer->ne_fc) ? 'checked' : '';
		$this->oForm->e_ok = (true !== $this->oRegistry->Viewer->ne_ok) ? 'checked' : '';

		return $this;
	}
}
